<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
</head>
<body>
<div class="addBrand-container" id="user-add-app">
    <div class="container">
        <el-form
                ref="ruleForm"
                :model="ruleForm"
                :rules="rules"
                :inline="false"
                label-width="180px"
                class="demo-ruleForm"
        >
            <el-form-item
                    label="单据详情:">
                <el-input
                        v-model="ruleForm.information"
                        maxlength="20"/>
            </el-form-item>
            <el-form-item
                    label="供应商名称:">
                <el-input
                        v-model="ruleForm.company"
                        maxlength="20"
                />
            </el-form-item>

            <el-form-item
                    label="来源单据:">
                <el-input
                        v-model="ruleForm.contract"
                        maxlength="20"
                />
            </el-form-item>
            <el-form-item
                    label="应付账金额:">
                <el-input
                        v-model="ruleForm.money"
                        maxlength="20"
                />
            </el-form-item>
            <el-form-item
                    label="制单日期:">
                <div class="block">
                    <span class="demonstration"></span>
                    <el-date-picker
                            v-model="ruleForm.createDateTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择日期时间">
                    </el-date-picker>
                </div>
            </el-form-item>
            </el-form-item>

            <el-form-item
                    label="应付日期:">
                <div class="block">
                    <span class="demonstration"></span>
                    <el-date-picker
                            v-model="ruleForm.endDateTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择日期时间">
                    </el-date-picker>
                </div>
            </el-form-item>
            <div class="subBox address">
                <el-form-item>
                    <el-button @click="goBack()">
                        取消
                    </el-button>
                    <el-button
                            type="primary"
                            @click="submitForm('ruleForm', false)"
                    >
                        保存
                    </el-button>
                    <el-button
                            v-if="actionType == 'add'"
                            type="primary"
                            class="continue"
                            @click="submitForm('ruleForm', true)"
                    >
                        保存并继续添加
                    </el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/payable.js"></script>
<script src="../../js/validate.js"></script>
<script src="../../js/index.js"></script>
<script>
    new Vue({
        el: '#user-add-app',
        data() {
            return {
                value1: '',
                value2: '',
                id: '',
                actionType: '',
                ruleForm: {
                    'information': '',
                    'company': '',
                    'contract': '',
                    'money': '',
                    'createDateTime': '',
                    'endDateTime': '',
                    username: ''
                }
            }
        },
        computed: {
            rules() {
                return {
                    //账号
                    username: [
                        {
                            required: true, 'validator': checkUserName, trigger: 'blur'
                        }
                    ],
                    //姓名
                    name: [{required: true, 'validator': checkName, 'trigger': 'blur'}],
                    'phone': [{'required': true, 'validator': checkPhone, 'trigger': 'blur'}],
                    'idNumber': [{'required': true, 'validator': validID, 'trigger': 'blur'}]
                }
            }
        },
        created() {
            this.id = requestUrlParam('id')
            this.actionType = this.id ? 'edit' : 'add'
            if (this.id) {
                this.init()
            }
        },
        mounted() {
        },
        methods: {
            async init() {
                queryUserById(this.id).then(res => {
                    console.log(res)
                    if (String(res.code) === '1') {
                        console.log(res.data)
                        this.ruleForm = res.data
                        this.ruleForm.sex = res.data.sex === '0' ? '女' : '男'
                        // this.ruleForm.password = ''
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })
            },
            submitForm(formName, st) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.actionType === 'add') {
                            const params = {
                                ...this.ruleForm,
                            }
                            addPayable(params).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('添加成功！')
                                    if (!st) {
                                        this.goBack()
                                    } else {
                                        this.ruleForm = {
                                            'information': '',
                                            'company': '',
                                            'contract': '',
                                            'money': '',
                                            'createDateTime': '',
                                            'endDateTime': '',
                                            username: ''
                                        }
                                    }
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        } else {
                            const params = {
                                ...this.ruleForm,
                            }
                            editPayable(params).then(res => {
                                if (res.code === 1) {
                                    this.$message.success('信息修改成功！')
                                    this.goBack()
                                } else {
                                    this.$message.error(res.msg || '操作失败')
                                }
                            }).catch(err => {
                                this.$message.error('请求出错了：' + err)
                            })
                        }
                    } else {
                        console.log('error submit!!')
                        return false
                    }
                })
            },
            goBack() {
                window.parent.menuHandle({
                    id: '2',
                    url: '/backend/page/payable/list.html',
                    name: '应付账管理'
                }, false)
            }
        }
    })
</script>
</body>
</html>
